package com.heima.Servlet;

import com.heima.bean.Student;
import com.sun.org.apache.bcel.internal.generic.BREAKPOINT;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/ListStudentServlet")
public class ListStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先要读取本地的文件获取里面的数据
        BufferedReader  br = new BufferedReader(new FileReader("d://student.txt"));
        //创建一个list集合，把学生对象存入到里面再进行分割。
        ArrayList<Student> list = new ArrayList<>();
        String line;
        while ((line=br.readLine())!=null){
            Student student =new Student();
            String[] arr = line.split(",");
            student.setName(arr[0]);
            student.setAge(Integer.parseInt(arr[1]));
            student.setScore(Integer.parseInt(arr[2]));
            list.add(student);

        }
        //将集合对象存入会话域中
        req.getSession().setAttribute("student",list);
        //重定向到学生列表页面
        resp.sendRedirect(req.getContextPath()+"/listStudent.jsp");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
